package com.ldk.base.framework.datasource;

import javax.sql.DataSource;
import javax.sql.XADataSource;

import org.postgresql.xa.PGXADataSource;

import com.ibm.db2.jcc.DB2XADataSource;
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
/*import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;*/

import oracle.jdbc.xa.client.OracleXADataSource;

/**
 * XA DataSource数据工厂
 * 支持ORACLE MYSQL MSSQL POSTRESQL 数据库
 * @author xiangwl
 *
 */
public class CustomXADataSourceFactory extends BaseDataSourceFacotory{

	/**
	 * 创建数据库
	 * @param driver
	 * @para url
	 * @paramusername
	 * @param password
	 * @return
	 */
	public static DataSource createDataSource(DataSourceDriver driver, String uniqueResourceName, 
											  String serverName, int portNumber,String databaseName,
											  String user, String password){
		String url = createDbUrl(driver, serverName, portNumber, databaseName);
		//封装过的XADataSource
		CustomXADataSource customXADataSource = new CustomXADataSource(uniqueResourceName);
		
		XADataSource xaDataSource = null;
		try{
			if(driver == DataSourceDriver.ORACLE){
				OracleXADataSource oracleXADataSource = new OracleXADataSource();
				oracleXADataSource.setURL(url);
				oracleXADataSource.setUser(user);
				oracleXADataSource.setPassword(password);
				
				xaDataSource = oracleXADataSource;
			}
			else if(driver == DataSourceDriver.MYSQL){
				MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
				mysqlXADataSource.setURL(url);
				mysqlXADataSource.setUser(user);
				mysqlXADataSource.setPassword(password);
				
				xaDataSource = mysqlXADataSource;
			}
			else if(driver == DataSourceDriver.MSSQL){
				SQLServerXADataSource sqlServerXADataSource = new SQLServerXADataSource();
				sqlServerXADataSource.setURL(url);
				sqlServerXADataSource.setUser(user);
				sqlServerXADataSource.setPassword(password);
				
				xaDataSource = sqlServerXADataSource;
				
			}
			else if(driver == DataSourceDriver.POSTRESQL){
				PGXADataSource pgxaDataSource = new PGXADataSource();
				pgxaDataSource.setUrl(url);
				pgxaDataSource.setUser(user);
				pgxaDataSource.setPassword(password);
				
				xaDataSource = pgxaDataSource;
			}
			else if(driver == DataSourceDriver.DB2){
				DB2XADataSource db2XADataSource = new DB2XADataSource();
				db2XADataSource.setServerName(serverName);
				db2XADataSource.setPortNumber(portNumber);
				db2XADataSource.setDatabaseName(databaseName);
				db2XADataSource.setUser(user);
				db2XADataSource.setPassword(password);
				//driver type 4
				db2XADataSource.setDriverType(4);
				db2XADataSource.setCurrentSchema(databaseName);
				
				xaDataSource = db2XADataSource;
				//对于db2 testQuery 要重新设置下面语句为MDW库中表查询语句
				customXADataSource.setTestQuery(null);//"SELECT COUNT(*) FROM PRMDTA;"
			}
			else{
				customXADataSource.setXaDataSourceClassName(driver.getXaDriver());
			}
		}
		catch(Exception e){
			
		}
		if(xaDataSource == null)
			return null;
		//设置数据库源
		customXADataSource.setXaDataSource(xaDataSource);
		
		return customXADataSource;
	}
	
	
}
